package edu.pku.sei.modeler.uml.commands.usecase;

import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.exceptions.FactoryNotFoundException;
import edu.pku.sei.modeler.model.common.ModelRegistry;
import edu.pku.sei.modeler.model.uml.Kernel.Classifier;
import edu.pku.sei.modeler.model.uml.UseCases.*;
import edu.pku.sei.modeler.uml.modelHelper.ModelFactory;

public class CreateExtensionPointCommand extends Command {

	private Classifier classifier;

	// private Operation operation;
	private ExtensionPoint extensionPoint;

	public CreateExtensionPointCommand(Classifier classifier) {
		this.classifier = classifier;
		try {
			this.extensionPoint = ((ModelFactory) (ModelRegistry.getRegistry()
					.getFactory(classifier.getModel()))).createExtensionPoint();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (FactoryNotFoundException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean canExecute() {
		return classifier != null;
	}

	@Override
	public void execute() {
		if (this.classifier instanceof UseCase) {
			((UseCase)this.classifier).getExtensionPoint().add(this.extensionPoint);
			 this.extensionPoint.setContainer(classifier);
			 this.extensionPoint.setModel(classifier.getModel());
		}
	}

	@Override
	public boolean canUndo() {
		return classifier != null;
	}

	@Override
	public void undo() {
		if (this.classifier instanceof UseCase) {
			((UseCase)this.classifier).getExtensionPoint().remove(this.extensionPoint);
		}
	}

}
